package com.cyt.work.divideclass.service.impl;

import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.work.divideclass.dao.DivideclassClassDao;
import com.cyt.work.divideclass.dao.DivideclassStudentDao;
import com.cyt.work.divideclass.model.po.DivideclassClass;
import com.cyt.work.divideclass.model.po.DivideclassStudent;
import com.cyt.work.divideclass.model.vo.DivideclassClassVo;
import com.cyt.work.divideclass.model.vo.DivideclassStudentVo;
import com.cyt.work.divideclass.service.DivideclassClassService;
import com.cyt.work.divideclass.service.DivideclassStudentService;
import com.cyt.work.enrollment.model.vo.EnrollmentHomeinfoVo;
import com.cyt.work.wb.model.po.WxWbBulid;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 智能分班分班设置-智能分班分班设置Service层实现类
 *
 * @author caideyong
 * @date 2019-12-20 14:11:03
 */
@Service("divideclassClassService")
public class DivideclassClassServiceImpl extends BaseServiceImpl<DivideclassClass> implements DivideclassClassService {

    @Resource
    private DivideclassClassDao divideclassClassDao;

    @Autowired
    public void setBaseDao(DivideclassClassDao divideclassClassDao) {
        this.baseDao = divideclassClassDao;
    }

    @Autowired
    private DivideclassStudentDao divideclassStudentDao;

    @Autowired
    private DivideclassStudentService divideclassStudentService;

    /**
     * 查询对象集合
     *
     * @param divideclassClassVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @Override
    public List<DivideclassClass> listExample(DivideclassClassVo divideclassClassVo) {
        return divideclassClassDao.getDivideclassClassList(divideclassClassVo);
    }

    /**
     * 分页查询
     *
     * @param divideclassClassVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @Override
    public ReturnResult page(DivideclassClassVo divideclassClassVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<DivideclassClass> list = listExample(divideclassClassVo);
        PageInfo<DivideclassClass> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param divideclassClassVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(DivideclassClassVo divideclassClassVo, SysUser sysUser) {
        if (StringUtils.isBlank(divideclassClassVo.getId())) {
            DivideclassClass divideclassClass = new DivideclassClass(sysUser);
            divideclassClass.setName(divideclassClassVo.getName());
            divideclassClass.setNum(divideclassClassVo.getNum());
            divideclassClass.setHeadTeacher(divideclassClassVo.getHeadTeacher());
            divideclassClass.setRuleId(divideclassClassVo.getRuleId());
            divideclassClass.setCreator(divideclassClassVo.getCreator());
            divideclassClass.setBnum(divideclassClassVo.getBnum());
            divideclassClass.setGnum(divideclassClassVo.getGnum());
            divideclassClass.setAverage(divideclassClassVo.getAverage());
            divideclassClass.setDivideId(divideclassClassVo.getDivideId());
            divideclassClassDao.insert(divideclassClass);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            DivideclassClass divideclassClass = divideclassClassDao.selectByPrimaryKey(divideclassClassVo.getId());
            divideclassClass.setName(divideclassClassVo.getName());
            divideclassClass.setNum(divideclassClassVo.getNum());
            divideclassClass.setHeadTeacher(divideclassClassVo.getHeadTeacher());
            divideclassClass.setRuleId(divideclassClassVo.getRuleId());
            divideclassClass.setCreator(divideclassClassVo.getCreator());
            divideclassClass.setBnum(divideclassClassVo.getBnum());
            divideclassClass.setGnum(divideclassClassVo.getGnum());
            divideclassClass.setAverage(divideclassClassVo.getAverage());
            divideclassClass.setDivideId(divideclassClassVo.getDivideId());
            divideclassClassDao.updateByPrimaryKey(divideclassClass);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult changeClass(DivideclassClassVo divideclassClassVo, SysUser sysUser) {
        if(StringUtils.isNotEmpty(divideclassClassVo.getStuId())){
            String stu[]=divideclassClassVo.getStuId().split(",");
            Integer bNum=0;
            Integer gNum=0;
            String oldClass="";
            for(int i=0;i<stu.length;i++){
                DivideclassStudent divideclassStudent=divideclassStudentDao.selectByPrimaryKey(stu[i]);
                oldClass=divideclassStudent.getClassId();
                divideclassStudent.setClassId(divideclassClassVo.getId());
                if(divideclassStudent.getSex().equals("1")){
                    bNum+=1;
                }else{
                    gNum+=1;
                }
                divideclassStudentDao.updateByPrimaryKey(divideclassStudent);
            }
            //修改班级
            DivideclassClass divideclassClass1=divideclassClassDao.selectByPrimaryKey(oldClass);
            divideclassClass1.setNum((Integer.parseInt(divideclassClass1.getNum())-stu.length)+"");
            divideclassClass1.setBnum((Integer.parseInt(divideclassClass1.getBnum())-bNum)+"");
            divideclassClass1.setGnum((Integer.parseInt(divideclassClass1.getGnum())-gNum)+"");
            DivideclassStudentVo divideclassStudentVo=new DivideclassStudentVo();
            divideclassStudentVo.setClassId(oldClass);
            List<DivideclassStudent> d1=divideclassStudentService.listExample(divideclassStudentVo);
            Integer total=0;
            for(DivideclassStudent d:d1){
                total+=Integer.parseInt(d.getTotalScore());
            }
            divideclassClass1.setAverage(total/d1.size()+"");
            divideclassClassDao.updateByPrimaryKey(divideclassClass1);
            //修改班级
            DivideclassClass divideclassClass2=divideclassClassDao.selectByPrimaryKey(divideclassClassVo.getId());
            Integer total2=0;
            DivideclassStudentVo divideclassStudentVo1=new DivideclassStudentVo();
            divideclassStudentVo1.setClassId(divideclassClassVo.getId());
            List<DivideclassStudent> d2=divideclassStudentService.listExample(divideclassStudentVo1);
            for(DivideclassStudent d:d2){
                total2+=Integer.parseInt(d.getTotalScore());
            }
            divideclassClass2.setAverage(total2/d2.size()+"");
            divideclassClass2.setNum((Integer.parseInt(divideclassClass2.getNum())+stu.length)+"");
            divideclassClass2.setBnum((Integer.parseInt(divideclassClass2.getBnum())+bNum)+"");
            divideclassClass2.setGnum((Integer.parseInt(divideclassClass2.getGnum())+gNum)+"");
            divideclassClassDao.updateByPrimaryKey(divideclassClass2);
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
    }

    /**
     * 根据条件逻辑删除
     *
     * @param divideclassClassVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(DivideclassClassVo divideclassClassVo, SysUser sysUser) {
        //要修改的字段
        DivideclassClass divideclassClass = new DivideclassClass();
        divideclassClass.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        //筛选条件
        Example example = new Example(DivideclassClass.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(divideclassClassVo.getId())) {
            criteria.andIn("id", Arrays.asList(divideclassClassVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        divideclassClassDao.updateByExampleSelective(divideclassClass, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param divideclassClassVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(DivideclassClassVo divideclassClassVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(DivideclassClass.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(divideclassClassVo.getId())) {
            criteria.andIn("id", Arrays.asList(divideclassClassVo.getId().split(",")));
        }
        if(StringUtils.isNotEmpty(divideclassClassVo.getDivideId())){
            criteria.andEqualTo("divideId",divideclassClassVo.getDivideId());
        }
        divideclassClassDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public void export(HttpServletResponse response, DivideclassClassVo divideclassClassVo) {
        List<DivideclassClass> divideclassClassList=listExample(divideclassClassVo);
        EasyPoiUtil.exportExcel(divideclassClassList,"智能分班班级表","智能分班班级表",DivideclassClass.class,"智能分班班级表.xls",response);
    }

    @Override
    public void insertList(List<DivideclassClass> divideclassClassList) {
        divideclassClassDao.insertList(divideclassClassList);
    }

    @Override
    public ReturnResult dTreeListClass(DivideclassClassVo divideclassClassVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<DivideclassClass> divideclassClassList = this.listExample(divideclassClassVo);//applyId,divideId
        if(divideclassClassList!=null&&divideclassClassList.size()>0){
            DivideclassClassVo divideclassClassVo1=new DivideclassClassVo();
            divideclassClassVo1.setId(divideclassClassList.get(0).getParentId());
            List<DivideclassClass> divideclassClassList1=this.listExample(divideclassClassVo1);
            DivideclassClass divideclassClass1 =divideclassClassList1.get(0);
            divideclassClassList.add(divideclassClass1);
            for (DivideclassClass divideclassClass : divideclassClassList) {
                Map<String, Object> resultMap = new LinkedHashMap<>();
                resultMap.put("id", divideclassClass.getId());
                resultMap.put("title", divideclassClass.getName());
                resultMap.put("parentId", StringUtils.isEmpty(divideclassClass.getParentId()) ? "0" : divideclassClass.getParentId());
                Map<String, String> map = new LinkedHashMap<>();
                map.put("type", "0");
                map.put("isChecked", "0");
                List<Map<String, String>> checkArrList = new ArrayList<>();
                checkArrList.add(map);
                resultMap.put("checkArr", checkArrList);
                resultList.add(resultMap);
            }
        }
        return new ReturnResult(resultList);
    }
}